package de.lazy.gui.debugframe;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class DebugFrame extends JFrame{

	private JPanel container;
	
	private int elementWidth;
	
	public DebugFrame(int x,int y,int width,int height){
		this.elementWidth = width;
		
		setVisible(true);
		setTitle("DebugFrame");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setBounds(x, y, width, height);
		setPreferredSize(new Dimension(width, height));
		
		container = new JPanel();
		container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
		container.add(new JLabel(""));
		container.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		
		JScrollPane listScroller = new JScrollPane(container);
		listScroller.setPreferredSize(new Dimension(250, 80));
		listScroller.setAlignmentX(LEFT_ALIGNMENT);
		
		setContentPane(container);
		pack();
		
	}
	
	public JSlider addSliderElement(int min, int max, int initial, String valueTitle, final IntegerValueHandler handler){
		//container
		JPanel elementContainer = new JPanel();
		elementContainer.setLayout(new BorderLayout());
		elementContainer.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
		elementContainer.setMaximumSize(new Dimension(2000, 80));
		
		//slider
		final JSlider element = new JSlider(JSlider.HORIZONTAL, min, max, initial);
		double ticks = ((double)max-(double)min)/5;
		element.setMajorTickSpacing((int)ticks);
		element.setPaintTicks(true);
		element.setPaintLabels(true);
		elementContainer.add(element,BorderLayout.CENTER);
		
		//current value
		final JTextField currentValue = new JTextField(new Integer(initial).toString(),5);
		//currentValue.set
		currentValue.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try{
					element.setValue(Integer.parseInt(currentValue.getText()));
				}
				catch(NumberFormatException e){
					JOptionPane.showMessageDialog(container, "Invalid Value!\nMessage - " +e.getMessage());
				}
			}
		});
		
		element.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent arg0) {
				JSlider source = (JSlider)arg0.getSource();
				currentValue.setText(new Integer(source.getValue()).toString());
				handler.valueChanged(source.getValue());
			}
		});
		elementContainer.add(currentValue,BorderLayout.EAST);
		
		//headline container
		JPanel header = new JPanel();
		header.setLayout(new BorderLayout());
		header.add(new JLabel(valueTitle),BorderLayout.CENTER);
		header.add(currentValue,BorderLayout.EAST);
		
		elementContainer.add(header,BorderLayout.NORTH);
		
		container.add(elementContainer);
		pack();
		return element;
	}
	
}
